//
//  ST_PushPeculiarityViewController.swift
//  btvideo
//
//  Created by mac on 2020/1/13.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import HandyJSON
import SwifterSwift

//发布特质-级 Controller
class ST_PushPeculiarityViewController: ST_BTLightBarViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        view.backgroundColor = UIColor.white

        title_nav_btn.setTitleForAllStates("选择特质类型")
        
         
        
        view.addSubview(collectionView)
        collectionView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.init(top: statusBarHeight+49, left: 0, bottom: 0, right: 0))
        }
        
        request_data()
    }
    

    func request_data(){
        
        JVrequest(PH.peculiarityListForRegisterApi, success: { (json) in
            
            if let list = [ST_PeculiarityModel].deserialize(from: json["data"].rawString() ?? "") as? [ST_PeculiarityModel]{
                
                self.data_array = list
            }
            
            self.collectionView.reloadData()
            
        }) { (code, err) in
            
            ToastHud(text: err, view: self.view)
        }
        
        
        
    }
    
//    MARK: - Properties

    fileprivate lazy var layout:UICollectionViewFlowLayout = {
       let controlLayout = UICollectionViewFlowLayout()
        controlLayout.minimumLineSpacing = 15
        controlLayout.minimumInteritemSpacing = 15
        controlLayout.sectionInset = UIEdgeInsets.init(top: 0, left: 15, bottom: 0, right: 15)
        
        let w = (ScreenWidth - 15 * 3)/2
        
        controlLayout.itemSize = CGSize.init(width: w.int.cgFloat, height: w.int.cgFloat)
        
        return controlLayout
    }()
    
    fileprivate lazy var collectionView:UICollectionView = {
        let control = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: self.layout)
        control.delegate = self
        control.dataSource = self
        control.register(cellWithClass: ST_PushST_PeculiarityCell.self)
        if #available(iOS 11.0, *) {
            control.contentInsetAdjustmentBehavior = .never
        }
        control.contentInset = UIEdgeInsets.init(top: 0, left: 0, bottom: BTSafeAreaBottom, right: 0)
        
        control.alwaysBounceVertical = true
        
        control.backgroundColor = Color.white
        control.showsVerticalScrollIndicator = false
        control.showsHorizontalScrollIndicator = false
        return control
    }()
    
    fileprivate lazy var data_array = [ST_PeculiarityModel]()
}

extension ST_PushPeculiarityViewController:UICollectionViewDelegate,UICollectionViewDataSource{


    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return data_array.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withClass: ST_PushST_PeculiarityCell.self, for: indexPath)
        
        if let m = data_array[safe:indexPath.item]{
            cell.model = m
        }
        
        return cell
    }


    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        willPushParentPeculiarity = data_array[safe:indexPath.item]
        
        self.navigationController?.pushViewController(ST_PushPeculiarityChildrenController())
        
    }

}

